import _curses
import curses
import curses.ascii
import types


attrs = {
        'bold':curses.A_BOLD,
        'highlight':curses.A_REVERSE,
        'normal':curses.A_NORMAL,
        'blink':curses.A_BLINK,
        'dim':curses.A_DIM,
        'bright':curses.A_STANDOUT,
        'underline':curses.A_UNDERLINE
        }

keymap = { 9   : "tab",
               10  : "enter",
               27  : { 79 : { 77 : "enter",
                              80 : "F1",
                              81 : "F2",
                              82 : "F3",
                              83 : "F4",
                              109: "-",
                              107: "+"},
                      None: "esc", # if we get a 27, followed by nothing,
                                   # it's an ESC
                      27: "esc",
                      91  : { 49  : { 49  :  { 126 : "F1"},
                                      50  : { 126 : "F2"},
                                      51  : { 126 : "F3"},
                                      52  : { 126 : "F4"},
                                      53  : { 126 : "F5"},
                                      55  : { 126 : "F6" },
                                      56  : { 126 : "F7" },
                                      57  : { 126 : "F8" },
                                      126 : "home" },
                              50  : { 48  : { 126 : "F9" },
                                      49  : { 126 : "F10" },
                                      51  : { 126 : "F11" },
                                      52  : { 126 : "F12" },
                                      126 : "insert" },
                              51  : { 126 : "delete" },
                              52  : { 126 : "end" },
                              53  : { 126 : "page up" },
                              54  : { 126 : "page down" },
                              55  : { 126 : "home" },
                              56  : { 126 : "end" },
                              65  : "up",
                              66  : "down",
                              67  : "right",
                              68  : "left",
                              70  : "end",
                              72  : "home",
                              90  : "tab",
                              91  : { 65  : "F1",
                                      66  : "F2",
                                      67  : "F3",
                                      68  : "F4",
                                      69  : "F5" } } },
               32  : "space",
               127 : "backspace",

               # occasionally (though maybe not any more, or not
               # on all systems) we get non-7 bit inputs... so
               # we handle them just in case
               258 : "down",
               259 : "up",
               260 : "left",
               261 : "right",
               265 : "F1",
               266 : "F2",
               267 : "F3",
               268 : "F4",
               269 : "F5",
               270 : "F6",
               271 : "F7",
               272 : "F8",
               273 : "F9",
               274 : "F10",
               275 : "F11",
               276 : "F12",
               330 : "delete",
               331 : "insert",
               338 : "page down",
               339 : "page up",
               } 

curmap = keymap

def parseKey(key):
    global curmap
    global keymap
    if key == -1:
        try:
            key = curmap[None]
        except KeyError:
            key = None
        # reset things
        curmap = keymap
        return key

    
    if key in curmap:
	if type(curmap[key])==types.DictType:
	    curmap = curmap[key]
	    return None
	else:
	    k = curmap[key]
	    curmap = keymap
	    return k 
    #elif curses.ascii.isprint(key):
    elif 1==1:
	curmap = keymap
	return chr(key)
    else:
	return None
